package com.tank.game.core;

import com.tank.game.api.Bullet;
import com.tank.game.api.BulletCollection;
import com.tank.game.api.Tank;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 缺省的子弹集合
 *
 * @author Zed
 */
public class DefaultBulletCollection implements BulletCollection {

    private List<Bullet> bullets;

    public DefaultBulletCollection(Builder builder) {
        this.bullets = builder.bullets == null ? Collections.emptyList() : builder.bullets;
    }


    @Override
    public List<Bullet> getBullets() {
        return this.bullets;
    }

    @Override
    public Bullet findBullet(Predicate<Bullet> filter) {
        return this.bullets.stream()
                .filter(filter)
                .findFirst()
                .orElse(null);
    }

    @Override
    public List<Bullet> getBullets(Predicate<Bullet> filter) {
        return this.bullets.stream()
                .filter(filter)
                .collect(Collectors.toList());
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static Builder newBuilder(Tank tank, int number) {
        Builder builder = new Builder();
        for (int i = 0; i < number; i++) {
        }
        return builder;
    }

    public static class Builder {
        private List<Bullet> bullets;

        /**
         * 添加子弹集合
         *
         * @param bullets {@link List}
         * @return {@link Builder}
         */
        public Builder bullets(List<Bullet> bullets) {
            this.bullets = bullets;
            return this;
        }

        public DefaultBulletCollection build() {
            return new DefaultBulletCollection(this);
        }
    }
}
